package com.atguigu.dga230315.meta.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga230315.meta.bean.TableMetaInfo;
import com.atguigu.dga230315.meta.bean.TableMetaInfoExtra;
import com.atguigu.dga230315.meta.bean.TableMetaInfoForQuery;
import com.atguigu.dga230315.meta.bean.TableMetaInfoVO;
import com.atguigu.dga230315.meta.service.TableMetaInfoExtraService;
import com.atguigu.dga230315.meta.service.TableMetaInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/tableMetaInfo")
public class TableMetaInfoController {

    @Autowired
    TableMetaInfoService tableMetaInfoService;

    @Autowired
    TableMetaInfoExtraService tableMetaInfoExtraService;

    @PostMapping("init-tables/{schemaName}/{assessDate}")
    public String updateTables(@PathVariable("schemaName") String schemaName, @PathVariable("assessDate") String assessDate) {
        tableMetaInfoService.initMetaInfo(assessDate, schemaName);
        return "success";
    }

    @GetMapping("/table-list")
    public String tableList(TableMetaInfoForQuery tableMetaInfoForQuery) {
        //1.获取tableMetaInfo和tableMetaInfoExtra的数据 想办法把这两张表的数据关联起来
        //方式一：join   因为第一种不需要获取太多数据库链接
        //方式二：先查询出来某张表的数据然后一个个对另一个表中查询对应的数据  需要频繁的链接数据库
        List<TableMetaInfoVO> tableMetaInfoList = tableMetaInfoService.getTableMetaInfoList(tableMetaInfoForQuery);
        Integer total = tableMetaInfoService.getTableMetaInfoCount(tableMetaInfoForQuery);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", total);
        jsonObject.put("list", tableMetaInfoList);

        //或者封装到Map集合中
 /*       HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("total", total);
        hashMap.put("list", tableMetaInfoList);

        String jsonStr = JSON.toJSONString(hashMap);*/

        return jsonObject.toJSONString();

    }

    //单表信息详情
    @GetMapping("table/{tableId}")
    public String tableDetail(@PathVariable("tableId") String tableId) {
        //方式二：先查询出来某张表的数据然后一个个对另一个表中查询对应的数据  需要频繁的链接数据库(因为一次请求只查询一条数据因此不会频繁链接数据库)
        //1.根据id查询TableMetaInfo对象   根据id查询出来的数据必然最多只有一行 因此不需要再根据时间获取最新的数据
        TableMetaInfo tableMetaInfo = tableMetaInfoService.getOne(new QueryWrapper<TableMetaInfo>().eq("id", tableId));

        //2.查询到指定表的辅助信息数据  根据表名和库名查询出对应的辅助信息数据
        TableMetaInfoExtra tableMetaInfoExtra = tableMetaInfoExtraService.getOne(new QueryWrapper<TableMetaInfoExtra>().eq("table_name", tableMetaInfo.getTableName()).eq("schema_name", tableMetaInfo.getSchemaName()));

        //3.将辅助信息数据设置到TableMetaInfo对象中
        tableMetaInfo.setTableMetaInfoExtra(tableMetaInfoExtra);

        //将TableMetaInfo对象转为Json字符串返回
        return JSON.toJSONString(tableMetaInfo);
    }


    //更新辅助信息
    @PostMapping("tableExtra")
    public String updateTableMetaInfoExtra(@RequestBody TableMetaInfoExtra tableMetaInfoExtra) {
        tableMetaInfoExtra.setUpdateTime(new Date());
        //保存更新到数据库中
        tableMetaInfoExtraService.saveOrUpdate(tableMetaInfoExtra);
        return "success";
    }

}
